import React from 'react';
import { Empty } from 'antd';
import { PageContainer } from '@/molecules';
import { FilePreview } from './containers';

const getLocalItem = id => {
    if (id) {
        const nowItem = localStorage.getItem('localDeclare');
        let obj = null;
        if (nowItem) {
            try {
                obj = JSON.parse(nowItem);
            } catch (error) {
                console.log(error);
            }
        }
        return obj?.[id] || null;
    }
    return null;
};

const LawPreview = ({ location }) => {
    const { query } = location;
    const { id } = query;
    const declareObj = React.useMemo(() => getLocalItem(id), [id]);

    return (
        <PageContainer title="法律条款">
            {declareObj?.file ? (
                <FilePreview title="法律条款" url={declareObj?.file} />
            ) : (
                <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="未读取到相关文件" />
            )}
        </PageContainer>
    );
};

export default LawPreview;
